package com.yxy.springcloud.framework.unionauth.service;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.yxy.springcloud.framework.common.base.BusinessException;
import com.yxy.springcloud.framework.common.base.PageDataList;
import com.yxy.springcloud.framework.unionauth.DTO.AppSystemSettingQueryDto;
import com.yxy.springcloud.framework.unionauth.DTO.DataDicQueryDto;
import com.yxy.springcloud.framework.unionauth.DTO.GetParamValueQueryDTO;
import com.yxy.springcloud.framework.unionauth.dao.AppSystemSettingMapper;
import com.yxy.springcloud.framework.unionauth.model.AppSystemSetting;
import com.yxy.springcloud.framework.unionauth.model.AppSystemSetting;
import com.yxy.springcloud.framework.unionauth.model.AuthAdminUser;
import com.yxy.springcloud.framework.unionauth.model.InsertBaseEntity;
import com.yxy.springcloud.framework.unionauth.utils.TokenCheckUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.*;

@Service
@Slf4j
public class AppSystemSettingService {

  @Autowired
  private TokenCheckUtil tokenCheckUtil;

  @Resource
  private AppSystemSettingMapper appSystemSettingMapper;

  public List<AppSystemSetting> getList(@Valid AppSystemSettingQueryDto query) {
    PageHelper.startPage(query.getPageNumber(), query.getPageSize());
    PageHelper.orderBy("t1.create_date desc");//排序
    List<AppSystemSetting> dbResult = appSystemSettingMapper.selectList(query);

    return dbResult;
  }

  @Transactional(rollbackFor = Exception.class)
  public Map<String, Object> createOrUpdate(AppSystemSetting inputDto) throws BusinessException {
    Map<String, Object> resultMap = new HashMap<>();
    String curUserStr = tokenCheckUtil.getUserInfoByToken();
    AuthAdminUser loginUser = JSON.parseObject(curUserStr, AuthAdminUser.class);

    Long id = inputDto.getId();
    if (id == null) { // insert
      AppSystemSetting insertObj = new AppSystemSetting();
      InsertBaseEntity baseEntity = new InsertBaseEntity(loginUser.getId(), loginUser.getName());
      BeanUtils.copyProperties(baseEntity, insertObj);
      BeanUtils.copyProperties(inputDto, insertObj);
      appSystemSettingMapper.insertSelective(insertObj);
      resultMap.put("id", insertObj.getId());
    } else { //update
      AppSystemSetting updateObj = new AppSystemSetting();
      BeanUtils.copyProperties(inputDto, updateObj);
      updateObj.setId(id);
      updateObj.setUpdateUserId(loginUser.getId());
      updateObj.setUpdateUserName(loginUser.getName());
      updateObj.setUpdateDate(new Date());
      appSystemSettingMapper.updateByPrimaryKeySelective(updateObj);
      resultMap.put("id", id);
    }
    return resultMap;
  }

  //物理删除
  public void delete(long id) throws BusinessException {
    appSystemSettingMapper.deleteByPrimaryKey(id);
  }

  public String getParamValue(GetParamValueQueryDTO inputDto) {
    AppSystemSetting queryObj = new AppSystemSetting();
    BeanUtils.copyProperties(inputDto, queryObj);
    queryObj.setStatus(0L);
    AppSystemSetting dbObj = appSystemSettingMapper.selectOne(queryObj);
    if (dbObj == null) {
      return null;
    } else {
      return dbObj.getParamValue();
    }
  }
}
